package fr.greta.centreFormation.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import fr.greta.centreFormation.bean.Info;

public class InfoHome {
	
	public static ArrayList<Info> selectAll(){
		
		ArrayList<Info> collectionInfo = new ArrayList<Info>();
		String selectAll = "SELECT * FROM info";
		
		try {
			Connection connection = Bdd.getConnection();
			PreparedStatement stmt = connection.prepareStatement(selectAll);
			ResultSet resultat = stmt.executeQuery();
			
			while(resultat.next()){
				
				int idInfo = resultat.getInt(1);
				Date dateInfo = resultat.getDate(2);
				String intituleInfo = resultat.getString(3);
				String contenuInfo = resultat.getString(4);
				
				Info i = new Info(idInfo, dateInfo, intituleInfo, contenuInfo);
				collectionInfo.add(i);
				
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return collectionInfo;
	}
	
	public static Info selectInfoBy(int idinfo){
		
		Info info = new Info();
		String select = "SELECT * FROM info WHERE id_info = ?";
		
		try {
			Connection connection = Bdd.getConnection();
			PreparedStatement stmt = connection.prepareStatement(select);
			stmt.setInt(1, idinfo);
			ResultSet resultat = stmt.executeQuery();
			resultat.next();
			
			info.setIdInfo(resultat.getInt(1));
			info.setDateInfo(resultat.getDate(2));
			info.setIntituleInfo(resultat.getString(3));
			info.setContenuInfo(resultat.getString(4));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
		
		
	}
	
	public static Boolean insertInfo(Info info){
		
		Date dateInfo = info.getDateInfo();
		java.sql.Date sqlDateInfo = new java.sql.Date(dateInfo.getTime());
		
		String intituleInfo = info.getContenuInfo();
		String contenuInfo = info.getIntituleInfo();
		
		String insert = "INSERT INTO info (date_info, intitule_info, contenu_info) " +
				"VALUES (?, ?, ?)";
	
		try {
			Connection connection = Bdd.getConnection();
			PreparedStatement stmt = connection.prepareStatement(insert);
			stmt.setDate(1, sqlDateInfo);
			stmt.setString(2, intituleInfo);
			stmt.setString(3, contenuInfo);
			stmt.executeUpdate();
			return true;
			
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public static Boolean updateInfo(Info info){
		
		int idInfo = info.getIdInfo();
		
		Date dateInfo = info.getDateInfo();
		java.sql.Date sqlDateInfo = new java.sql.Date(dateInfo.getTime());
		
		String intituleInfo = info.getIntituleInfo();
		String contenuInfo = info.getContenuInfo();
		
		String update = "UPDATE info SET date_info = ?, intitule_info = ?, contenu_info = ? WHERE id_info = ?";
		
		try {
			Connection connection = Bdd.getConnection();
			PreparedStatement stmt = connection.prepareStatement(update);
			stmt.setDate(1, sqlDateInfo);
			stmt.setString(2, intituleInfo);
			stmt.setString(3, contenuInfo);
			stmt.setInt(4, idInfo);
			stmt.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public static boolean deleteInfo(Info info){
		
		int idInfo = info.getIdInfo();
		
		String delete = "DELETE FROM info WHERE id_info = ?";
		
		try {
			Connection connection = Bdd.getConnection();
			PreparedStatement stmt = connection.prepareStatement(delete);
			stmt.setInt(1, idInfo);
			stmt.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
		
		
	}
	

}
